package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DistributorMgtExcution;
import com.fortune.dto.in.distributorMgt.DAccountAddDto;
import com.fortune.dto.in.distributorMgt.DAccountEditDto;
import com.fortune.dto.in.distributorMgt.DAccountListDto;
import com.fortune.dto.in.distributorMgt.UserUpAndDownDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.DistributorMgtEnum;
import com.fortune.service.DAccountInfoService;
import com.fortune.vo.distributorMgt.Distributor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "distributorMgt", description = "经销商管理相关", tags = {"经销商管理相关"})
@RestController
@RequestMapping("distributorMgt")
public class DistributorMgtController {
	@Autowired
	private DAccountInfoService dAccountInfoService;

	@ApiOperation(value = "经销商列表", nickname = "distributorList", notes = "", tags={ "@石匡" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<Distributor>> list(HttpServletRequest request, @ApiIgnore @Validated DAccountListDto dAccountListDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		dAccountListDto.setPage((dAccountListDto.getPage()-1)*10);
		List<Distributor> distributorList = dAccountInfoService.queryAccountList(companyId,dAccountListDto);
		int count = dAccountInfoService.queryAccountListCount(companyId,dAccountListDto);
		PagingData<Distributor> pagingData = new PagingData<>();
		pagingData.setData(distributorList);
		pagingData.setRecordsTotal(count);
		return new DistributorMgtExcution<PagingData<Distributor>>(pagingData, DistributorMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "添加经销商", nickname = "dAccountAdd", notes = "d_postion经销商岗位岗位名称,说明默认为管理员,d_user_info经销商员工账户员工编号默认为手机号", tags={ "@石匡" })
	@PostMapping(value = "add",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody DAccountAddDto dAccountAddDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = dAccountInfoService.queryAccountIsExist(companyId,dAccountAddDto);
		if (num > 0){
			return new DistributorMgtExcution<Void>(null, DistributorMgtEnum.ACCOUNT_EXIST);
		}
		num = dAccountInfoService.queryAccountByNumber(dAccountAddDto.getPrincipalPhone());
		if (num > 0){
			return new DistributorMgtExcution<Void>(null,DistributorMgtEnum.ACCOUNT_EXIST);
		}
		num = dAccountInfoService.addAccount(companyId,dAccountAddDto);
		if (num < 1){
			return new DistributorMgtExcution<Void>(null,DistributorMgtEnum.FAILURE);
		}
		return new DistributorMgtExcution<Void>(null, DistributorMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "编辑经销商", nickname = "dAccountEdit", notes = "", tags={ "@石匡" })
	@PutMapping(value = "edit",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DAccountEditDto dAccountEditDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = dAccountInfoService.queryAccountById(companyId,dAccountEditDto.getId());
		if (num < 1){
			return new DistributorMgtExcution<Void>(null, DistributorMgtEnum.ACCOUNT_NO_EXIST);
		}
		num = dAccountInfoService.queryEditAccountIsExist(companyId,dAccountEditDto);
		if (num > 0){
			return new DistributorMgtExcution<Void>(null, DistributorMgtEnum.ACCOUNT_EXIST);
		}
		num = dAccountInfoService.queryEditAccountByPhone(companyId,dAccountEditDto);
		if (num > 0){
			return new DistributorMgtExcution<Void>(null,DistributorMgtEnum.ACCOUNT_EXIST);
		}
		num = dAccountInfoService.editAccountByDealerIdAndCompanyId(companyId,dAccountEditDto);
		if (num < 1){
			return new DistributorMgtExcution<Void>(null,DistributorMgtEnum.FAILURE);
		}
		return new DistributorMgtExcution<Void>(null,DistributorMgtEnum.SUCCESS);
	}


	@ApiOperation(value = "删除经销商", nickname = "dAccountDelete", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="acccountId", value="经销商ID",paramType="query",dataType="string")
	@DeleteMapping(value = "delete",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotEmpty(message="经销商ID不能为空") String acccountId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = dAccountInfoService.queryAccountById(companyId,acccountId);
		if (num < 1){
			return new DistributorMgtExcution<Void>(null,DistributorMgtEnum.ACCOUNT_NO_EXIST);
		}
		num = dAccountInfoService.deleteAccount(companyId,acccountId);
		if (num < 1){
			return new DistributorMgtExcution<Void>(null,DistributorMgtEnum.FAILURE);
		}
		return new DistributorMgtExcution<Void>(null,DistributorMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "重置经销商密码", nickname = "dAccountRsetPassword", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="acccountId", value="经销商ID",paramType="query",dataType="string")
	@PostMapping(value = "resetPassword",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> resetPassword(HttpServletRequest request,@Validated @NotEmpty(message="经销商ID不能为空") String acccountId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		String password = Constants.D_USER_PASSWORD;
		int num = dAccountInfoService.resetPassword(companyId,acccountId,password);
		if (num < 1){
			return new DistributorMgtExcution<Void>(null,DistributorMgtEnum.FAILURE);
		}
		return new DistributorMgtExcution<Void>(null, DistributorMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "账户状态", nickname = "dUserUpAndDown", notes = "@石匡")
	@PutMapping(value = "dUserUpAndDown",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> upAndDown(HttpServletRequest request,@Validated @RequestBody UserUpAndDownDto userUpAndDownDto){
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		Distributor distributorInfo = dAccountInfoService.queryAccountInfoById(companyId,userUpAndDownDto.getId());
		if (distributorInfo == null){
			return new DistributorMgtExcution<Void>(null, DistributorMgtEnum.ACCOUNT_NO_EXIST);
		}
		if (String.valueOf(userUpAndDownDto.getType()).equals("YES") && distributorInfo.getStatus().equals(userUpAndDownDto.getType())){
			return new DistributorMgtExcution<Void>(null, DistributorMgtEnum.ACCOUNT_ENABLE);
		}else if(String.valueOf(userUpAndDownDto.getType()).equals("NO") && distributorInfo.getStatus().equals(userUpAndDownDto.getType())){
			return new DistributorMgtExcution<Void>(null, DistributorMgtEnum.ACCOUNT_DISABLE);
		}else {
			int num = dAccountInfoService.upAndDownUser(companyId,userUpAndDownDto);
			if (num < 1){
				return new DistributorMgtExcution<Void>(null, DistributorMgtEnum.FAILURE);
			}
		}
		return new DistributorMgtExcution<Void>(null, DistributorMgtEnum.SUCCESS);
	}


}
